#include <account.h>
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
using namespace std;


account::account(string id,string pwd)
:ID(id),password(pwd){}

string account::Getid(void) const
{
    return ID;
}

string account::Getpassword(void) const
{
    return password;
}

void account::Setid(const string& id)
{   
    ID=id;
}

void account::SetPassword(const string& pwd)
{
    password=pwd;
}

Json::Value account::toJson() const
{
    Json::Value jsonData;
    jsonData["ID"] = ID;
    jsonData["Password"] = password;
    return jsonData;
}

void account::fromJson(const Json::Value& jsondata)
{
    ID = jsondata["ID"].asString();
    password = jsondata["Password"].asString();
}

AccountManager::AccountManager(string filename):filename(filename)
{
    Load();
}

void AccountManager::Load()
{
    ifstream file(filename);
    if(file.is_open())
    {
        Json::Value jsonData;
        Json::CharReaderBuilder reader;
        std::string errors;
        if (Json::parseFromStream(reader, file, &jsonData, &errors)) {
            for (const auto& item : jsonData) {
                account ac("", "");
                ac.fromJson(item);
                accounts.push_back(ac);
            }
        } else {
            cout << "无法解析账户文件 " << errors << std::endl;
        }
        file.close();
    }
    else 
    {
        std::cout << "缺少必要账户数据文件！请检查 " << std::endl;
    }
}
/**
 * @brief 添加账户
 * 
 * @param account 
 * @return true 添加创建成功
 * @return false 添加失败，账号已经存在
 */
bool AccountManager::Add_account(const account& account)
{
    char added=0;
    for (auto& item : accounts) {
        if (item.Getid() == account.Getid()) {
            added=1;
            return false;
        }
    }
    accounts.push_back(account);
    return true;
}

/**
 * @brief 账户删除
 * 
 * @param id 账户id
 * @return true 账户删除成功
 * @return false 账户删除失败，账号不存在
 */
bool AccountManager::Del_account(const string& id)
{
    char deled=0;
    for (auto it = accounts.begin(); it != accounts.end(); ++it) {
        if ((*it).Getid() == id) {
            accounts.erase(it);
            deled=1;
            break;
        }
    }

    return deled;
}

/**
 * @brief 账户密码修改
 * 
 * @param id 账户id
 * @param password 
 * @return true 密码修改成功
 * @return false 密码修改失败，账户不存在
 */
bool AccountManager::Modify_Password(const string& id,const string& password)
{
    char flag=0;
    for (auto& account : accounts) {
        if (account.Getid() == id) {
            account.SetPassword(password);
            flag=1;
            break;
        }
    }
    return flag;
}

void AccountManager::Save() const
{
    ofstream file(filename);
    if (file.is_open()) {
        Json::Value jsonData;
        for (const auto& account : accounts) {
            jsonData.append(account.toJson());
        }
        file << jsonData;
        file.close();
    } else {
        cout << "无法打开账户数据文件 " << filename << endl;
    }
}

void AccountManager::List() const
{
    for (const auto& account : accounts) {
        cout << "ID: " << account.Getid() << endl;
    }
}

bool AccountManager::Verify(const string& id,const string& password)
{
    for (const auto& account : accounts) {
        if (account.Getid() == id && account.Getpassword() == password) {
            return true;
        }
    }
    return false;
}
